﻿package com.thorqq.magictimer;

import java.text.DecimalFormat;

import com.thorqq.magictimer.R;
import com.thorqq.magictimer.download.DownloadProxy;
import com.thorqq.magictimer.download.Downloader;
import com.thorqq.magictimer.download.TaskInfo;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class AdapterDownloadTask extends BaseAdapter
{

    public final class ViewHolder
    {
        public Button btnStartOrStop;
        public ImageView ivDownloadState;
        public TextView tvName;
        public TextView tvRemark;
        public TextView tvSpeed;
        public ProgressBar pbProgress;
        
        public DownloadActivity.DownloadClickListener longListener;
    }
    
    private LayoutInflater mInflater;
    private SparseArray<TaskInfo> mData;
    private Context mContext;
    private DownloadProxy mProxy = DownloadProxy.getInstance();
    
    private DownloadActivity mActivity;
    
    public AdapterDownloadTask(Context context, SparseArray<TaskInfo> data)
    {
        mInflater = LayoutInflater.from(context);
        mData = data;
        mContext = context;
    }

    public int getCount()
    {
        return mData.size();
    }

    public Object getItem(int position)
    {
        return mData.get(mData.keyAt(position));
    }

    public long getItemId(int position)
    {
        return mData.keyAt(position);
    }
    
    public void setActivity(DownloadActivity a)
    {
        mActivity = a;
    }

    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;

        final TaskInfo task = (TaskInfo) this.getItem(position);    
        
        if (convertView == null)
        {
            holder = new ViewHolder();

            // 布局
            convertView = mInflater.inflate(R.layout.download_task_item, null);
            holder.btnStartOrStop = (Button) convertView.findViewById(R.id.btnStartOrStopTask);
            holder.ivDownloadState = (ImageView) convertView.findViewById(R.id.ivDownloadState);
            holder.tvName = (TextView) convertView.findViewById(R.id.tvTaskName);
            holder.tvRemark = (TextView) convertView.findViewById(R.id.tvTaskRemark);
            holder.tvSpeed = (TextView) convertView.findViewById(R.id.tvTaskSpeed);
            holder.pbProgress = (ProgressBar) convertView.findViewById(R.id.pbTaskProgress);

            //启动/停止
            holder.btnStartOrStop.setOnClickListener(new View.OnClickListener()
            {
                
                @Override
                public void onClick(View v)
                {
                    int taskid = ((TaskInfo) getItem(position)).getTaskid();
                    mProxy.initDownloader(taskid);
                    mProxy.startOrStop(taskid);

                }
            });
                        
            //长按菜单监听器
            holder.longListener = mActivity.new DownloadClickListener(mContext, task);
            
            convertView.setTag(holder);
        } 
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        
        holder.longListener.setTag(task);
        convertView.setOnClickListener(holder.longListener);

        holder.tvName.setText(task.getLocalFile().substring(task.getLocalFile().lastIndexOf("/") + 1));

        if(task.getState() == Downloader.DL_STATE_INIT
                || holder.pbProgress.getMax() != task.getFileSize())
        {
            holder.pbProgress.setMax(task.getFileSize());
        }
        
        holder.pbProgress.setProgress(task.getComplete());
        holder.tvRemark.setText(String.format("%.2f%%(%.2fMB/%.2fMB)", 
                task.getComplete()*100.0f/task.getFileSize(),
                task.getComplete()*1.0f/1024/1024,
                task.getFileSize()*1.0f/1024/1024));
        
        DecimalFormat formater = new DecimalFormat("#0.00");
        holder.tvSpeed.setText(formater.format(task.getSpeed()*1.0f/1024.0) + "KB/s");
        
        if(mProxy.isDownloading(task.getTaskid()))
        {
            holder.btnStartOrStop.setText("暂停");
        }
        else
        {
            holder.btnStartOrStop.setText("继续");
        }
        
        if(task.getState() == Downloader.DL_STATE_COMPLETED
                || task.getComplete() >= task.getFileSize())
        {
            holder.btnStartOrStop.setVisibility(View.INVISIBLE);
        }
        else
        {
            holder.btnStartOrStop.setVisibility(View.VISIBLE);
        }

        if(task.getState() == Downloader.DL_STATE_COMPLETED
                || task.getComplete() >= task.getFileSize())
        {
            holder.ivDownloadState.setImageResource(R.drawable.downlaod_completed);
            holder.tvSpeed.setVisibility(View.GONE);
        }
        else if(task.getState() == Downloader.DL_STATE_PAUSE)
        {
            holder.btnStartOrStop.setText("继续");
            holder.ivDownloadState.setImageResource(R.drawable.download_paused);
            holder.tvSpeed.setVisibility(View.GONE);
        }
        else if(task.getState() == Downloader.DL_STATE_ERROR)
        {
            holder.btnStartOrStop.setText("继续");
            holder.ivDownloadState.setImageResource(R.drawable.download_error);
            holder.tvSpeed.setText("发生错误");
        }
        else if(task.getState() == Downloader.DL_STATE_DOWNLOADING)
        {
            holder.btnStartOrStop.setText("暂停");
            holder.ivDownloadState.setImageResource(R.drawable.download_downloading);
            holder.tvSpeed.setVisibility(View.VISIBLE);
        }
        
        if(mProxy.isToStart(task.getTaskid()))
        {
            holder.tvSpeed.setVisibility(View.VISIBLE);
            holder.tvSpeed.setText("连接中");
        }
        
        if(mProxy.isToPause(task.getTaskid()))
        {
            holder.tvSpeed.setVisibility(View.VISIBLE);
            holder.tvSpeed.setText("暂停中");
        }

        return convertView;
    }


}
